package model;

import java.util.Enumeration;
import java.util.Hashtable;

public class ShoppingCartModel {
	public Hashtable items = new Hashtable();
	public void themSP(String masp,String tensp,int giaban,int soluong){
		String[] item = {masp,tensp,Integer.toString(giaban),Integer.toString(soluong)};
		if(items.containsKey(masp)){
			String[] tmpsp = (String[])items.get(masp);
			int tmpsl = Integer.parseInt(tmpsp[3]);
			soluong+= tmpsl;
			tmpsp[3] = Integer.toString(soluong);
		}
		else{
			items.put(masp, item);
		}
	}
	public void xoasp(String masp){
		if(items.containsKey(masp)){
			items.remove(masp);
		}
	}
	public void updatesoluong(String masp,int soluong ){
		String[] tmpsp = (String[])items.get(masp);
		tmpsp[3] = Integer.toString(soluong);
	}
	public Enumeration getEnumeration(){
		return items.elements();
	}
	public int getCost(){
		Enumeration enum1 = items.elements();
		String[] tmpsp;
		int totalcost = 0;
		while(enum1.hasMoreElements())
		{
			tmpsp = (String[])enum1.nextElement();
			totalcost +=(Integer.parseInt(tmpsp[3]))* Integer.parseInt(tmpsp[2]);
		}
		return totalcost;
	}
	public int getNumOfItems(){
		Enumeration enum1 = items.elements();
		String[] tmpsp;
		int numOfItems = 0;
		while(enum1.hasMoreElements()){
			tmpsp = (String[])enum1.nextElement();
			numOfItems += Integer.parseInt(tmpsp[3]);
			
		}
		return numOfItems;
		
	}
	public void clearItems(){
		items.clear();
	}

}
